<?php

/**
 * Project: Espolon
 * Licence: BSD License
 * Link: http://espolon.org/
 * Copyright: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Author: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Version: Beta
 */

if (!defined('SITE_ROOT')) exit('No direct script access allowed');

class SignUpForm extends HTML_QuickForm {
  private $tpl;
  private $user;

  function SignUpForm($tpl,$user) {
    $this->tpl = $tpl;
    $this->user = $user;

    parent::HTML_QuickForm('signupForm','post','','','onsubmit="javascript:password1.value=hex_sha1(password1.value), password2.value=hex_sha1(password2.value)"');
    $this->addElement('header', 'header', 'signup');
		$this->addElement('text', 'alias', _('alias'), array('size' => 15, 'maxlength' => 25));
    $this->addElement('text', 'email1', _('Email address'), array('size' => 30, 'maxlength' => 100));
		$this->addElement('text', 'email2', _('Retype email address'), array('size' => 30, 'maxlength' => 100));
    $this->addElement('password', 'password1', _('Enter a password'), array('size' => 30, 'maxlength' => 50));
    $this->addElement('password', 'password2', _('Retype password'), array('size' => 30, 'maxlength' => 50));
    $this->addElement('submit', 'btn_Submit', 'Enviar', array('class' => 'btn_submit','onsubmit' => 'reset()'));
    $this->applyFilter('__ALL__', 'trim');
    $this->registerRule('check_exists_email','function','checkExistsEmail', $this);
    $this->addRule('email1', _('Required field cannot be left blank'), 'required');
    $this->addRule('email1', _('verify your email'), 'email');
    $this->addRule('email1', _('email already exists'), 'check_exists_email', $this->user);
		$this->addRule('email2', _('Required field cannot be left blank'), 'required');
    $this->addRule('email2', _('verify your email'), 'email');
		$this->addRule(array('email1', 'email2'), _('Email addresses do not match.'), 'compare');
    $this->addRule('password1', _('Required field cannot be left blank'), 'required');
    $this->addRule('password1', _('min lenght 4'), 'minlength', 4);
    $this->addRule('password2', _('Required field cannot be left blank'), 'required');
    $this->addRule('password2', _('min lenght 4'), 'minlength', 4);
		$this->addRule(array('password1', 'password2'), _('passwords do not match. Please re-enter your password correctly.'), 'compare');
  }

  function checkExistsEmail() {
    $this->user->setEmail($this->getSubmitValue('email1'));
    return $this->user->checkExists($post='email');
  }

  function displayForm() {
    $this->renderer = new HTML_QuickForm_Renderer_ArraySmarty($this->tpl);
    $this->renderer->setRequiredTemplate(
  		    '{if $error}
  		 	     <font color="red">{$label}</font>
  	 	     {else}
  	 		     {$label}
  		 	     {if $required}
  	            <font color="red" size="1">*</font>
  	   	     {/if}
  	       {/if}'
    );
    $this->renderer->setErrorTemplate(
          '{if $error}
  	  	    <font color="blue" size="1">{$error}</font><br />
  	       {/if}{$html}'
    );
    $this->setRequiredNote(
   		'<small class="red">*&nbsp;</small><small class="blue">'._('required fields').'</small>'
   	);

   	$this->accept($this->renderer);
   	$this->tpl->assign('form_data',$this->renderer->toArray());
   	$this->tpl->assign('msg',$this->user->getMsg());
  }

  function process() {
    $this->user->setEmailSubject(_('Sign up confirmation code'));
    $this->user->setEmailBody(_('Welcome to __sitename__, for activating your account follow the next link: __url__'));
    $this->user->createSignup($this->getSubmitValues());
    $this->tpl->assign('result',true);
    $this->tpl->assign('msg',$this->user->getMsg());
  }

}

?>